﻿using Abide_Halo_Library.IO;
using System;
using System.Windows.Forms;

namespace Abide.Forms
{
    public partial class HistoryForm : Form
    {
        public HistoryForm(MapEdit[] history)
        {
            InitializeComponent();

            //Add ListItems
            ListViewItem ListItem = null;

            foreach (MapEdit Edit in history)
            {
                ListItem = new ListViewItem();
                ListItem.Tag = Edit;
                ListItem.Text = Edit.offset.ToString();
                ListItem.SubItems.Add(Edit.length.ToString());

                switch (Edit.length)
                {
                    case 1: //Byte (or SByte)
                        ListItem.SubItems.Add(Edit.original[0].ToString());
                        ListItem.SubItems.Add(Edit.edit[0].ToString());
                        break;

                    case 2: //Short or UShort
                        ListItem.SubItems.Add(BitConverter.ToUInt16(Edit.original, 0).ToString());
                        ListItem.SubItems.Add(BitConverter.ToUInt16(Edit.edit, 0).ToString());
                        break;

                    case 4: //Int or UInt
                        ListItem.SubItems.Add(BitConverter.ToInt32(Edit.original, 0).ToString());
                        ListItem.SubItems.Add(BitConverter.ToInt32(Edit.edit, 0).ToString());
                        break;

                    case 8: //Long or ULong
                        ListItem.SubItems.Add(BitConverter.ToInt64(Edit.original, 0).ToString());
                        ListItem.SubItems.Add(BitConverter.ToInt64(Edit.edit, 0).ToString());
                        break;

                    default:
                        ListItem.SubItems.Add(string.Format("System.Byte[{0}]", Edit.length));
                        ListItem.SubItems.Add(string.Format("System.Byte[{0}]", Edit.length));
                        break;
                }
                
                HistoryList.Items.Add(ListItem);
            }

            //Show
            this.Show();
        }
    }
}
